package com.live.dengdengtv.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;

import com.live.dengdengtv.R;

import java.util.ArrayList;

/**
 * Created by wyf on 2016/8/11.
 */
public class MaritalStatusDialog {


    private Dialog pickDialog;
    private Context context;
//    private int chechedId = -1;
    private TextView chechedView = null;

//    private OnMaritalStatusDialogDismiss dialogDismiss;
    private OnItemSelected onItemSelected;
    private ArrayList<View> buttons;

    public void showPhotoDialog(Activity activity,int selected, OnItemSelected onItemSelected) {
        context = activity;
        this.onItemSelected = onItemSelected;
        pickDialog = new Dialog(activity, R.style.dialog_float);
        pickDialog.setContentView(R.layout.dialog_maritalstatus_choose);

        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        Window dlgwin = pickDialog.getWindow();
        WindowManager.LayoutParams lp = dlgwin.getAttributes();
        dlgwin.setGravity(Gravity.BOTTOM);
        lp.width = (int)(display.getWidth()); //设置宽度

        pickDialog.getWindow().setAttributes(lp);

        Button button1 = (Button) pickDialog.findViewById(R.id.button1);
        Button button2 = (Button) pickDialog.findViewById(R.id.button2);
        Button button3 = (Button) pickDialog.findViewById(R.id.button3);
        Button button4 = (Button) pickDialog.findViewById(R.id.button4);
        Button button5 = (Button) pickDialog.findViewById(R.id.button5);
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        button3.setOnClickListener(onClickListener);
        button4.setOnClickListener(onClickListener);
        button5.setOnClickListener(onClickListener);
        buttons = new ArrayList<View>();
        buttons.add(button1);
        buttons.add(button2);
        buttons.add(button3);
        buttons.add(button4);
        buttons.add(button5);

        pickDialog.show();
        if(selected>0){
            setCheck((TextView) buttons.get(selected));
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            setCheck((TextView) view);

            int selectIndex = -1;
//            switch (view.getId()){
//                case R.id.button1:  selectIndex = 0; break;
//                case R.id.button2:  selectIndex = 1; break;
//                case R.id.button3:  selectIndex = 2; break;
//                case R.id.button4:  selectIndex = 3; break;
//                case R.id.button5:  selectIndex = 4; break;
//            }

            selectIndex = buttons.indexOf(view);
            onItemSelected.onItemSelected(selectIndex);

            pickDialog.dismiss();
            context = null;
            onItemSelected = null;
            buttons.clear();
            buttons = null;
        }
    };

    private void setCheck(TextView view){
        if(chechedView != null){
            setCheckedState(chechedView, false);
        }

        chechedView = view;
        if(view != null){
            setCheckedState(view, true);
        }
    }

    private void setCheckedState(TextView view, boolean checked){

        int textColor = R.color.appText_gray_deep;
        int background = R.drawable.bg_corner_oval_grey_translucent;
        if(checked){
            textColor = R.color.appTextColor_white;
            background = R.drawable.bg_corner_oval_red;
        }
        view.setTextColor(context.getResources().getColor(textColor));
        view.setBackgroundResource(background);
    }

//    public interface OnMaritalStatusDialogDismiss {
//        public void onMaritalStatusDialogDismiss();
//    }

    public interface OnItemSelected {
        public void onItemSelected(int selectIndex);
    }

}
